package org.example.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.example.springboot.common.page.PageResult;
import org.example.springboot.common.service.impl.BaseServiceImpl;
import org.example.springboot.convert.IrrigationStatConvert;
import org.example.springboot.dao.IrrigationStatDao;
import org.example.springboot.entity.IrrigationStat;
import org.example.springboot.query.IrrigationStatQuery;
import org.example.springboot.service.IrrigationStatService;
import org.example.springboot.vo.IrrigationStatVO;
import org.springframework.stereotype.Service;

/**
 * @author Fz
 * @since 1.0.0 2024-08-05
 */
@Service
@AllArgsConstructor
public class IrrigationStatServiceImpl extends BaseServiceImpl<IrrigationStatDao, IrrigationStat> implements IrrigationStatService {

    @Override
    public PageResult<IrrigationStatVO> page(IrrigationStatQuery query) {
        IPage<IrrigationStat> page = this.page(query, getWrapper(query));
        return new PageResult<>(IrrigationStatConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    private LambdaQueryWrapper<IrrigationStat> getWrapper(IrrigationStatQuery query) {
        LambdaQueryWrapper<IrrigationStat> wrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(query.getUnitCode())) {
            wrapper.eq(IrrigationStat::getUnitCode, query.getUnitCode());
        }
        if (StringUtils.isNotBlank(query.getStartDate()) && StringUtils.isNotBlank(query.getEndDate())) {
            wrapper.between(IrrigationStat::getCreateDate, query.getStartDate(), query.getEndDate());
        }
        wrapper.orderByDesc(IrrigationStat::getCreateDate);
        return wrapper;
    }

}